import asyncio

import requests as requests
from pyppeteer import launch
from openpyxl import Workbook
import bisect


async def main():
    # proxy = requests.get('http://proxy.jiayincredit.net/random').text  # 代理服务器地址和端口
    # print(f'proxy:{proxy}')
    # browser = await launch({'args': ['--proxy-server=http://' + proxy], 'headless': False})
    browser = await launch({'headless': False})
    page = await browser.newPage()
    await page.goto('http://www.dongchedi.com/auto/params-carIds-x-4302')
    # await page.waitForNavigation()
    await asyncio.sleep(5)



    # allseries_ids = requests.get('https://www.dongchedi.com/motor/pc/car/brand/get_brand_series_list?aid=1839&app_name=auto_web_pc&brand_id=2&no_sales=0&show_historical_series=1&category_group_optimize=true').json()




    # 获取信息参数值
    # title = await page.evaluate('document.title')
    # print(f'title:{title}')
    await page.waitForSelector('#__next')
    elements = await page.xpath('//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label')
    values = []
    for i, element in enumerate(elements):
        value = await page.evaluate('(element) => element.textContent', element)
        values.append(value)
    print(values)  # 输出所有获取到的值

    numPro = len(values)
    print(f'numPro属性总数为:{numPro}')


    #获取车型
    elements_chexing = await page.xpath('//*[@id="__next"]/div/div/div/div[2]/div[2]/div[1]/div[1]/div/div/h1/a')
    values_chexing = []
    for i, element_chexing in enumerate(elements_chexing):
        value = await page.evaluate('(element) => element.textContent', element_chexing)
        values_chexing.append(value)
    print(f'车型：{values_chexing}')  # 输出所有获取到的值

    #写入excel
    # book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    wb = Workbook()
    # sheet = book.add_sheet('懂车帝数据', cell_overwrite_ok=True)
    # 选择活动工作表
    ws = wb.active


    #第一辆车的车型
    elements1 = await page.xpath(
        f'//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label/parent::div/parent::div/div[2]')
    values1 = []
    teshu = []
    teshu11 = []
    for iss, element1 in enumerate(elements1):
        childElement = await element1.querySelectorAll('div')

        if len(childElement) == 1:
            value1 = await page.evaluate('(element) => element.textContent', childElement[0])
            values2 = value1
        else:
            values3 = []
            for isss, ele in enumerate(childElement):
                childElements = await ele.querySelectorAll('div')
                for isss22, ele2222 in enumerate(childElements):
                    childElements222 = await ele2222.querySelectorAll('div')
                    for isss33, ele3333 in enumerate(childElements222):
                        value1 = await page.evaluate('(element) => element.textContent', ele3333)
                        values3.append(value1)
            N = len(values_chexing)
            teshu_dict = dict()
            teshu_dict['xuhao'] =iss
            teshu11.append(iss)
            teshu_dict['values3'] = values3
            teshu.append(teshu_dict)
            new_lists = [values3[i2::N] for i2 in range(N)]
            new_lists1 = new_lists[0]
            values2 = ', '.join(new_lists1)
        values1.append(values2)
    print(f'values1:{values1}')
    print(f'teshu:{teshu}')
    print(f'teshu_changdu:{len(teshu)}')
    for ioooo in range(0, len(values1)):
        ws.cell(row=2, column=ioooo + 2, value=values1[ioooo])
    #初始化values1
    # values1 = []

    # 后面的车型
    for i1, chexing in enumerate(values_chexing[1:]):
        # print(f'正在获取：{chexing}的配置')
        values1 = []
        elements_after = await page.xpath(
            f'//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label/parent::div/parent::div/div[{i1+3}]')
        for index1 in range(numPro):
            #0,1,2,3...numPro-1
            # 使用bisect_left找到插入位置
            position = bisect.bisect_left(teshu11, index1)
            if index1 in teshu11:
                result = [d['values3'] for d in teshu if d['xuhao'] == index1]
                result = result[0]
                new_lists = [result[i2::N] for i2 in range(N)]
                new_lists1 = new_lists[i1+1]
                values2 = ', '.join(new_lists1)
                values1.append(values2)
            else:
                element1 = elements_after[index1-position]
                value1 = await page.evaluate('(element) => element.textContent', element1)
                values1.append(value1)
        for ioooo in range(0, len(values1)):
            ws.cell(row=i1+3, column=ioooo + 2, value=values1[ioooo])
        print(f'车型{chexing}的配置为：{values1}')



    #       # 输出所有获取到的值
    #     for ioooo in range(0, len(values1)):
    #         # 渲染各个车型数据
    #         # sheet.write(i+1, ioooo + 1, values1[ioooo])
    #         ws.cell(row=i+2, column=ioooo + 2, value=values1[ioooo])
    #渲染表头列
    for i in range(0, len(values)):
        # sheet.write(0, i+1, values[i])
        ws.cell(row=1, column=i+2, value=values[i])

    #渲染车型
    col_chexing = tuple(values_chexing)
    for i in range(0, len(col_chexing)):
        # sheet.write(i+1, 0, col_chexing[i])
        ws.cell(row=i+2, column=1, value=col_chexing[i])
    #
    #
    # # book.save('懂车帝奥迪A7.xlsx')
    wb.save('懂车帝奥迪A7L.xlsx')




    # await page.screenshot({'path': 'example.png'})
    await browser.close()


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
